package com.szsfm.vehiclemanagement.terminal;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.posapi.PosApi;

import com.orhanobut.logger.Logger;
import com.szsfm.vehiclemanagement.interfaces.IPrinter;
import com.szsfm.vehiclemanagement.util.ToastUtil;

import java.io.File;
import java.io.FileWriter;

/**
 * Created by zhoubigbo on 2017/11/21.
 */
public class PadPrinter implements IPrinter {

    PosApi mPosApi = null;

    private PadPrinter() {
    }

    public PadPrinter(Context context) {
        mPosApi = PosApi.getInstance(context);

        //1  单片机上电
        try {
            FileWriter localFileWriterOn = new FileWriter(new File("/proc/gpiocontrol/set_sam"));
            localFileWriterOn.write("1");
            localFileWriterOn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        //设置初始化回调
        mPosApi.setOnComEventListener(onCommEventListener);
        //使用扩展方式初始化接口
        mPosApi.initDeviceEx("/dev/ttyMT2");
        IntentFilter mFilter = new IntentFilter();
        mFilter.addAction(PosApi.ACTION_POS_COMM_STATUS);
        context.registerReceiver(mReceiver, mFilter);
    }

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            String action = intent.getAction();
            if (action.equalsIgnoreCase(PosApi.ACTION_POS_COMM_STATUS)) {
                int cmdFlag = intent.getIntExtra(PosApi.KEY_CMD_FLAG, -1);
                int status = intent.getIntExtra(PosApi.KEY_CMD_STATUS, -1);

                if (cmdFlag == PosApi.POS_PRINT_PICTURE || cmdFlag == PosApi.POS_PRINT_TEXT) {

                    switch (status) {
                        case PosApi.ERR_POS_PRINT_SUCC:
                            //打印成功
                            Logger.d("打印成功");
                            ToastUtil.show("打印成功");
                            break;
                        case PosApi.ERR_POS_PRINT_NO_PAPER:
                            //打印缺纸
                            Logger.d("打印缺纸");
                            ToastUtil.show("打印缺纸");
                            break;
                        case PosApi.ERR_POS_PRINT_FAILED:
                            //打印失败
                            ToastUtil.show("打印失败");
                            break;
                        case PosApi.ERR_POS_PRINT_VOLTAGE_LOW:
                            //电压过低
                            ToastUtil.show("电压过低");
                            break;
                        case PosApi.ERR_POS_PRINT_VOLTAGE_HIGH:
                            //电压过高
                            ToastUtil.show("电压过高");
                            break;

                    }

                }
                if (cmdFlag == PosApi.POS_PRINT_GET_STATE) {
                }

                if (cmdFlag == PosApi.POS_PRINT_SETTING) {
                }

            }
        }

    };

    PosApi.OnCommEventListener onCommEventListener = new PosApi.OnCommEventListener() {
        @Override
        public void onCommState(int cmdFlag, int state, byte[] resp, int respLen) {
            switch (cmdFlag) {
                case PosApi.POS_INIT:
                    if (state == PosApi.COMM_STATUS_SUCCESS) {
                        Logger.d("初始化成功");
                    } else {
                        Logger.d("初始化失败");
                    }
                    break;
            }
        }
    };

    @Override
    public void printText(String s) {
        byte[] buf = null;
        try {
            buf = s.toString().getBytes("GBK");
        } catch (Exception e) {
            Logger.d("" + e.getMessage());
        }
        if (buf == null) {
            Logger.d("buf==null");
            return;
        }
        //1倍字体大小
        //byte[] _1x = new byte[]{0x1b, 0x57, 0x01};
        byte[] _2x = new byte[]{0x1b,0x57,0x01};
        byte[] mData = new byte[3 + buf.length];
        //1倍字体大小  默认
        System.arraycopy(_2x, 0, mData, 0, _2x.length);
        System.arraycopy(buf, 0, mData, _2x.length, buf.length);
        mPosApi.printText(25, mData, mData.length);
    }

    /**
     *
     * //2倍字体大小
     byte[] _2x = new byte[]{0x1b,0x57,0x02};
     *
     *
     */
}
